package mars.activity;

import bear.download.Msgread;
import bear.download.Nameread;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class User extends TabActivity {
private Button return1,logout,message,f5;
private TabHost tabhost;
private TextView roll0,roll1,roll2,roll3,roll4,roll5,username;
private ScrollView scroll;
private int ori=0;
public static boolean active=false,newmessage=false;
public static String namezhu;    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		active= true;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user);
		tabhost = (TabHost)findViewById(android.R.id.tabhost);
        tabhost.setup();
		logout=(Button)findViewById(R.id.logout);
	    roll0=(TextView)findViewById(R.id.roll0);
		roll1=(TextView)findViewById(R.id.roll1);
		roll2=(TextView)findViewById(R.id.roll2);
		roll3=(TextView)findViewById(R.id.roll3);
		roll4=(TextView)findViewById(R.id.roll4);
		roll5=(TextView)findViewById(R.id.roll5);
		return1=(Button)findViewById(R.id.return1);
		message=(Button)findViewById(R.id.message);
		f5=(Button)findViewById(R.id.f5);
		scroll = (ScrollView)findViewById(R.id.sv);
		username = (TextView)findViewById(R.id.username);
		
		username.setText(Nameread.username);
		scroll.setVerticalScrollBarEnabled(false);
		
		Intent intentbook=new Intent();
		intentbook.setClass(this,bookActivity.class);
		Intent intentfriend=new Intent();
		intentfriend.setClass(this,friendActivity.class);
		Intent intentshare=new Intent();
        intentshare.setClass(this,shareActivity.class);
        
		tabhost.addTab(tabhost.newTabSpec("tab_1")
		         .setContent(intentbook).setIndicator(null,getResources().getDrawable(R.drawable.android1)));
		tabhost.addTab(tabhost.newTabSpec("tab_2")
		         .setContent(intentfriend).setIndicator(null,getResources().getDrawable(R.drawable.phonebook)));
		        tabhost.addTab(tabhost.newTabSpec("tab_3")
		        .setContent(intentshare).setIndicator(null,getResources().getDrawable(R.drawable.wikipedia)));
		       tabhost.setCurrentTab(1);		        
		        
				String senders[] = Msgread.sender1.split("\\$");
				String contents[] = Msgread.content1.split("\\$");
				String reads[] = Msgread.read1.split("\\$");
					if(Msgread.n>6||Msgread.n==6){
					roll0.setText(senders[Msgread.n-1]+":"+contents[Msgread.n-1]);
					roll1.setText(senders[Msgread.n-2]+":"+contents[Msgread.n-2]);
					roll2.setText(senders[Msgread.n-3]+":"+contents[Msgread.n-3]);
					roll3.setText(senders[Msgread.n-4]+":"+contents[Msgread.n-4]);
					roll4.setText(senders[Msgread.n-5]+":"+contents[Msgread.n-5]);
					roll5.setText(senders[Msgread.n-6]+":"+contents[Msgread.n-6]);
					}else if(Msgread.n==5){
						roll0.setText(senders[Msgread.n-1]+":"+contents[Msgread.n-1]);
						roll1.setText(senders[Msgread.n-2]+":"+contents[Msgread.n-2]);
						roll2.setText(senders[Msgread.n-3]+":"+contents[Msgread.n-3]);
						roll3.setText(senders[Msgread.n-4]+":"+contents[Msgread.n-4]);
						roll4.setText(senders[Msgread.n-5]+":"+contents[Msgread.n-5]);
						}else if(Msgread.n==4){
							roll0.setText(senders[Msgread.n-1]+":"+contents[Msgread.n-1]);
							roll1.setText(senders[Msgread.n-2]+":"+contents[Msgread.n-2]);
							roll2.setText(senders[Msgread.n-3]+":"+contents[Msgread.n-3]);
							roll3.setText(senders[Msgread.n-4]+":"+contents[Msgread.n-4]);
							}else if(Msgread.n==3){
								roll0.setText(senders[Msgread.n-1]+":"+contents[Msgread.n-1]);
								roll1.setText(senders[Msgread.n-2]+":"+contents[Msgread.n-2]);
								roll2.setText(senders[Msgread.n-3]+":"+contents[Msgread.n-3]);
								}else if(Msgread.n==2){
									roll0.setText(senders[Msgread.n-1]+":"+contents[Msgread.n-1]);
									roll1.setText(senders[Msgread.n-2]+":"+contents[Msgread.n-2]);
									}else if(Msgread.n==1){
										roll0.setText(senders[Msgread.n-1]+":"+contents[Msgread.n-1]);
									}else{
										roll0.setText(R.string.roll);
									}
					for(int i=0;i<Msgread.n;i++){
						if(reads[i].equals("0")){
							newmessage=true;
						}
					}
		        
		        if(newmessage==true){
		        message.setText(R.string.newmessage);
		        
		        final Animation animation = new AlphaAnimation(1, 0);
		        animation.setDuration(500);
		        animation.setInterpolator(new LinearInterpolator());
		        animation.setRepeatCount(Animation.INFINITE);
		        animation.setRepeatMode(Animation.REVERSE);
		        message.startAnimation(animation);
		        }
		        
		       tabhost.setOnTabChangedListener(new OnTabChangeListener()
		        {
		            // TODO Auto-generated method stub
		            public void onTabChanged(String tabId)
		            {
		            	if(tabId=="tab_1"){
		           Toast.makeText(User.this,R.string.tab1,Toast.LENGTH_SHORT).show();
		            }
		            	if(tabId=="tab_2"){
		 		           Toast.makeText(User.this,R.string.tab2,Toast.LENGTH_SHORT).show();
			            }
		            	if(tabId=="tab_3"){
		 		           Toast.makeText(User.this,R.string.tab3,Toast.LENGTH_SHORT).show();
		 		            }
		            }
		        });
		       f5.setOnClickListener(new Button.OnClickListener()
		        {
					public void onClick(View v) {
						// TODO Auto-generated method stub;
						Intent in=new Intent();
						in.setClass(User.this, Qidong.class);
						 User.this.startActivity(in);
						 finish();
					}	
		        });
		message.setOnClickListener(new Button.OnClickListener()
        {
			public void onClick(View v) {
				// TODO Auto-generated method stub;
				Intent intent=new Intent();
				intent.setClass(User.this, Msg.class);
				User.this.startActivity(intent);
				finish();
				v.clearAnimation();
				message.setText(R.string.message);
			}	
        });
		logout.setOnClickListener(new Button.OnClickListener()
	        {
				public void onClick(View v) {
					// TODO Auto-generated method stub
					AlertDialog dialog1 = new AlertDialog.Builder(User.this)  
		            .setIcon(android.R.drawable.btn_star_big_on)  
		            .setTitle(R.string.logout2)  
		          .setPositiveButton(R.string.determine, onclick2)  
		            .setNegativeButton(R.string.cancel,  onclick2).create();  
						dialog1.show(); 
				}	
	        });
		return1.setOnClickListener(new Button.OnClickListener()
        {
			public void onClick(View v) {
				// TODO Auto-generated method stub					
					Intent intent=new Intent();
					intent.setClass(User.this, Activity1.class);
					User.this.startActivity(intent);
					finish();
			}	
        });
	}
	@Override
		public boolean onCreateOptionsMenu(Menu menu) {
			// TODO Auto-generated method stub
			menu.add(0,1,1,R.string.exit);
			menu.add(0,2,2,R.string.options);
			menu.add(0,3,3,R.string.help);
			return super.onCreateOptionsMenu(menu);
		}
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
			// TODO Auto-generated method stub
			if(item.getItemId()==1){
				AlertDialog dialog = new AlertDialog.Builder(User.this)  
	            .setIcon(android.R.drawable.btn_star_big_on)  
	            .setTitle(R.string.tuichu)  
	          .setPositiveButton(R.string.determine, onclick)  
	            .setNegativeButton(R.string.cancel,  onclick).create();  
					dialog.show(); 
			}else if(item.getItemId()==2){
				item.setIntent(new Intent(this, Options.class));
			}else if(item.getItemId()==3){
				item.setIntent(new Intent(this, Help.class));
			}
			return super.onOptionsItemSelected(item);
		}
	private Handler message1 = new Handler(){   
        public void handleMessage(Message msg) {   
            doScrow();                     
            }
	};
    public class TimerLoop implements Runnable {   
        public void run() {   
            // TODO Auto-generated method stub                 
            while(true){   
                loop(250000);                     
                message1.sendEmptyMessage(0);   
            }   
        }              
    }     
    public void loop(long i){   
        long j = i;   
        while(j>0){                 
            j = j-1;   
        }     
    }    
    public boolean onKeyDown(int keyCode, KeyEvent msg){   
        Thread loop = new Thread(new TimerLoop());   
        loop.start();              
        return super.onKeyDown(keyCode, msg);   
    }  
    public void doScrow(){   
        int now = scroll.getScrollY(); 
        if(ori == now){   
            scroll.scrollTo(now, 0);   
            ori = -1;                  
        }   
        else {   
            scroll.smoothScrollBy(0,1);                  
            ori = now;                  
        }   
    } 
    OnClickListener onclick = new OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) {  
            switch (which) {  
                case Dialog.BUTTON_NEGATIVE:  
                    break;  
                case Dialog.BUTTON_POSITIVE:
                	Intent startMain = new Intent(Intent.ACTION_MAIN);
        			startMain.addCategory(Intent.CATEGORY_HOME);
        			startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        			startActivity(startMain);
        			System.exit(0);
                    break;  
            } 
        }
    };
    OnClickListener onclick2 = new OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) {  
            switch (which) {  
                case Dialog.BUTTON_NEGATIVE:  
                    break;  
                case Dialog.BUTTON_POSITIVE:
                	Intent intent=new Intent();
					intent.setClass(User.this, Activity1.class);
					User.this.startActivity(intent);
					active=false;
					System.exit(0);
                    break;  
            } 
        }
    };
}   


